home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / util / shell / ViNCEd.lha / ViNCEd / Include / Examples / ReadWindowSize.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-05  |  6.6 KB  |  160 lines

  1. /*********************************************************
  2.  ** ReadWindowSize                                      **
  3.  **                                                     **
  4.  ** An example source how to read the window size       **
  5.  ** in characters using CSI sequences (correctly)       **
  6.  ** © 03.07.2000, THOR                                  **
  7.  ** Thomas Richter                                      **
  8.  *********************************************************/
  9.  
  10. /// Includes
  11. #include <exec/types.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14.  
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. ///
  18. /// Defines
  19. #define COOKED_MODE     0
  20. #define RAW_MODE        1
  21. #define MAX_DELAY       (5*1000*1000)
  22. ///
  23. /// Protos
  24. BOOL WindowSize(BPTR file,LONG *width,LONG *height);
  25. ///
  26. /// Statics
  27. char version[]="$VER: ReadWindowSize 1.00 (4.7.2000) © THOR";
  28. struct DosLibrary *DOSBase;      /* This is filled in by the startup code */
  29. ///
  30.  
  31. /// Sample main program
  32. int main(int argc,char **argv)
  33. {
  34. LONG width,height;
  35.  
  36.         if (WindowSize(Output(),&width,&height)) {
  37.                 Printf("The window size is %ld x %ld characters.\n",width,height);
  38.                 return 0;
  39.         }
  40.  
  41.         Printf("Could not obtain the window size.\n");
  42.         return 5;
  43. }
  44. ///
  45. /// WindowSize
  46. BOOL WindowSize(BPTR file,LONG *width,LONG *height)
  47. {
  48. BOOL success;
  49. BOOL incsi,innum,negative,inesc;
  50. LONG counter;
  51. LONG args[5];
  52. UBYTE in;
  53.  
  54.  
  55.         *width = *height = 0;
  56.  
  57.         if (SetMode(file,RAW_MODE)) {
  58.  
  59.                 success  = TRUE;
  60.                 incsi    = FALSE;
  61.                 inesc    = FALSE;
  62.                 innum    = FALSE;
  63.                 negative = FALSE;
  64.                 counter  = 0;
  65.  
  66.                 /* Now send a window borders request to the stream */
  67.                 Write(file,"\2330 q",4);
  68.  
  69.                 /* Now parse an incomming string */
  70.                 for(;;) {
  71.                         if (WaitForChar(file,MAX_DELAY) == FALSE) {
  72.                                 success = FALSE;
  73.                                 break;
  74.                         }
  75.  
  76.                         if (Read(file,&in,1) != 1) {
  77.                                 success = FALSE;
  78.                                 break;
  79.                         }
  80.  
  81.                         if (incsi) {
  82.                                 if ((in<' ') || (in>'~')) {             /* Invalid sequence? */
  83.                                         incsi = FALSE;
  84.                                 } else if ((in>='0') && (in<='9')) {
  85.                                         /* Valid number? */
  86.                                         if (innum == FALSE) {
  87.                                                 innum = TRUE;
  88.                                                 args[counter] = 0;
  89.                                         }
  90.                                         args[counter] = args[counter]*10+in-'0';
  91.                                 } else {
  92.                                         /* Abort parsing the number. Install its sign, and let it be. */
  93.                                         if (innum) {
  94.                                                 if (negative)
  95.                                                         args[counter] = -args[counter];
  96.                                                 innum    = FALSE;
  97.                                                 negative = FALSE;
  98.                                         }
  99.                                         if ((in>='@') && (in<='~')) {    /* End of sequence? */
  100.                                                 if ((in=='r') && (counter==3)) {        /* Is it a bounds report? */
  101.                                                         *height = args[2]-args[0]+1;
  102.                                                         *width  = args[3]-args[1]+1;
  103.                                                         break;
  104.                                                 }
  105.                                                 incsi = FALSE;          /* Abort sequence */
  106.                                         } else if (in==';') {                   /* Argument separator? */
  107.                                                 counter++;
  108.                                                 if (counter>4) counter=4;       /* Do not parse more than 5 arguments, throw everything else away */
  109.                                                 innum    = FALSE;
  110.                                                 negative = FALSE;
  111.                                         } else if (in=='-') {
  112.                                                 if (innum)
  113.                                                         incsi = FALSE;  /* minus sign in the middle is invalid */
  114.                                                 negative = ~negative;
  115.                                         } else if (in==' ') {
  116.                                                 /* Ignore SPC prefix */
  117.                                         } else {
  118.                                                 /* Abort the sequence */
  119.                                                 incsi = FALSE;
  120.                                         }
  121.                                 }
  122.                         } else if (inesc) {
  123.                                 if (in == '[') {
  124.                                         inesc    = FALSE;
  125.                                         incsi    = TRUE;   /* found a CSI sequence */
  126.                                         innum    = FALSE;  /* but not yet a valid number */
  127.                                         negative = FALSE;
  128.                                         counter  = 0;
  129.                                         args[0]  = args[1]=args[2]=args[3]=args[4] = 1;
  130.                                 } else if ((in >= ' ') && (in <= '/')) {
  131.                                         /* ignore the ESC sequence contents */
  132.                                 } else {
  133.                                         inesc    = FALSE;  /* terminate the ESC sequence */
  134.                                 }
  135.                         } else if (in == 0x9B) {
  136.                                         incsi    = TRUE;   /* found a CSI sequence */
  137.                                         innum    = FALSE;  /* but not yet a valid number */
  138.                                         negative = FALSE;
  139.                                         counter  = 0;
  140.                                         args[0]  = args[1]=args[2]=args[3]=args[4] = 1;
  141.                         } else if (in == 0x1B) {
  142.                                         inesc    = TRUE;  /* found an ESC sequence */
  143.                         } /* Everything else is thrown away */
  144.                 }
  145.  
  146.                 if (SetMode(file,COOKED_MODE)) {
  147.                         return success;
  148.                 }
  149.         }
  150.  
  151.         return FALSE;
  152. }
  153. ///
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.